<?php
	/**
	 * [sendToMobile 发送接收短信]
	 * @return [type] [description]
	 */
	function sendToMobile($mobile = '',$type){
		if (empty($mobile)) {
			return;
		}else{
			Vendor('AliyunDayu.SmsDemo');
			// $SmsDemo = new \Think\SmsDemo();
			$SmsDemo = new \SmsDemo ();
			// 短信接口对接，并发送通知信息
			switch ($type) {
				case 1://通知管理员有新订单
					$res = $SmsDemo->sendSms($mobile,'SMS_150861476');
					break;

				case 2://重置密码
					$randStr = str_shuffle('1234567890'); //短信验证码提供的号码，如需英文再加ABCDabcd
    				$code = substr($randStr,0,6);//短信验证码6位
					$_SESSION['forgetcode'] = $code;

					$response = $SmsDemo->sendSms($mobile,'SMS_150861485',$code);

					$res = object_to_array($response);
					// print_r($response);
					break;
				
				default:
					# code...
					break;
			}
		}
		return $res['Code'];
	}

	function object_to_array($obj) {
	    $obj = (array)$obj;
	    foreach ($obj as $k => $v) {
	        if (gettype($v) == 'resource') {
	            return;
	        }
	        if (gettype($v) == 'object' || gettype($v) == 'array') {
	            $obj[$k] = (array)object_to_array($v);
	        }
	    }
	 
	    return $obj;
	}


	/**
	 * [func_allUserGuess 批量更新用户开奖]
	 * @return [type] [description]
	 */
	function func_allUserGuess(){

		$m_applytask = M('Applytask');
		$m_gamessc = M('Game_ssc');

		$where_apply['winlose'] = array('exp','is null');
		$where_apply['type'] = array('exp','is not null');
		$where_apply['code'] = array('exp','is not null');
		$where_apply['codenum'] = array('exp','is not null');
		$where_apply['opencode'] = array('exp','is null');
		$where_apply['user_guess'] = array('exp','is not null');

		$applytaskList = $m_applytask->where($where_apply)->order('id asc')->select();
		// echo "<pre>";print_r($applytaskList);die();
		foreach ($applytaskList as $key => $value) {
			// 查找对应的期数
			$where_game['code'] = $value['code'];
			$where_game['codenum'] = $value['codenum'];
			$gamesscInfo = $m_gamessc->where($where_game)->find();
			if (!empty($gamesscInfo['id'])) {//已开奖
				$where['id'] = $value['id'];
				$save_apply['opencode'] = $gamesscInfo['num'];
				$m_applytask->where($where)->save($save_apply);

				// 判断用户结果
                userWinOrLose($value['id']);
			}
		}
	}
	
	/**
	 * [show_dragon 显示长龙]
	 * @return [type] [description]
	 */
	function show_dragon(){
		$m_dragon = M('Dragon');
		$dragonList = $m_dragon->order('id asc')->select();
		// echo "<pre>";print_r($dragonList);
		$return = array();
		$i = 0;
		foreach ($dragonList as $key => $value) {
			if ($value['big'] >=3) {
				if ($value['id'] == 1) {
					$return[$i]['name'] = '总和';
				}elseif ($value['id'] == 2) {
					$return[$i]['name'] = '第一球';
				}elseif ($value['id'] == 3) {
					$return[$i]['name'] = '第二球';
				}elseif ($value['id'] == 4) {
					$return[$i]['name'] = '第三球';
				}elseif ($value['id'] == 5) {
					$return[$i]['name'] = '第四球';
				}elseif ($value['id'] == 6) {
					$return[$i]['name'] = '第五球';
				}

				$return[$i]['type'] = '大';
				$return[$i]['val'] = $value['big'];
				$i++;
			}

			if ($value['small'] >=3) {
				if ($value['id'] == 1) {
					$return[$i]['name'] = '总和';
				}elseif ($value['id'] == 2) {
					$return[$i]['name'] = '第一球';
				}elseif ($value['id'] == 3) {
					$return[$i]['name'] = '第二球';
				}elseif ($value['id'] == 4) {
					$return[$i]['name'] = '第三球';
				}elseif ($value['id'] == 5) {
					$return[$i]['name'] = '第四球';
				}elseif ($value['id'] == 6) {
					$return[$i]['name'] = '第五球';
				}
				$return[$i]['type'] = '小';
				$return[$i]['val'] = $value['small'];
				$i++;
			}

			if ($value['bill'] >=3) {
				if ($value['id'] == 1) {
					$return[$i]['name'] = '总和';
				}elseif ($value['id'] == 2) {
					$return[$i]['name'] = '第一球';
				}elseif ($value['id'] == 3) {
					$return[$i]['name'] = '第二球';
				}elseif ($value['id'] == 4) {
					$return[$i]['name'] = '第三球';
				}elseif ($value['id'] == 5) {
					$return[$i]['name'] = '第四球';
				}elseif ($value['id'] == 6) {
					$return[$i]['name'] = '第五球';
				}
				$return[$i]['type'] = '单';
				$return[$i]['val'] = $value['bill'];
				$i++;
			}

			if ($value['both'] >=3) {
				if ($value['id'] == 1) {
					$return[$i]['name'] = '总和';
				}elseif ($value['id'] == 2) {
					$return[$i]['name'] = '第一球';
				}elseif ($value['id'] == 3) {
					$return[$i]['name'] = '第二球';
				}elseif ($value['id'] == 4) {
					$return[$i]['name'] = '第三球';
				}elseif ($value['id'] == 5) {
					$return[$i]['name'] = '第四球';
				}elseif ($value['id'] == 6) {
					$return[$i]['name'] = '第五球';
				}
				$return[$i]['type'] = '双';
				$return[$i]['val'] = $value['both'];
				$i++;
			}
		}

		return $return;
	}
	
	/**
	 * [func_dragon 添加长龙]
	 * @return [type] [description]
	 */
	function func_dragon($opencode){
        $m_dragon = M('Dragon');

		$opencodeArr = explode(',', $opencode);

		// 总和
		$total = $opencodeArr['0']+$opencodeArr['1']+$opencodeArr['2']+$opencodeArr['3']+$opencodeArr['4'];

		$where_total['id'] = 1;
		$totalInfo = $m_dragon->where($where_total)->find();
		if ($total > 25) {
			$save_total['big'] = $totalInfo['big'] + 1;
			$save_total['small'] = 0;
		}else{
			$save_total['big'] = 0;
			$save_total['small'] = $totalInfo['small'] + 1;
		}

		if ($total % 2 == 1) {
			$save_total['bill'] = $totalInfo['bill'] + 1;
			$save_total['both'] = 0;
		}else{
			$save_total['bill'] = 0;
			$save_total['both'] = $totalInfo['both'] + 1;
		}
		$m_dragon->where($where_total)->save($save_total);

		
		foreach ($opencodeArr as $key => $value) {
			$ball = $value;
			$where_ball['id'] = $key + 2;
			$ballInfo = $m_dragon->where($where_ball)->find();
			if ($ball > 4) {
				$save_ball['big'] = $ballInfo['big'] + 1;
				$save_ball['small'] = 0;
			}else{
				$save_ball['big'] = 0;
				$save_ball['small'] = $ballInfo['small'] + 1;
			}

			if ($ball % 2 == 1) {
				$save_ball['bill'] = $ballInfo['bill'] + 1;
				$save_ball['both'] = 0;
			}else{
				$save_ball['bill'] = 0;
				$save_ball['both'] = $ballInfo['both'] + 1;
			}
			$m_dragon->where($where_ball)->save($save_ball);
		}
		return;
	}


	/**
	 * [userWinOrLose 判断用户结果]
	 * @param  [type] $applytask_id [申请任务id]
	 * @return [type]               [description]
	 */
	function userWinOrLose($applytask_id){
		
		$m_applytask = M("Applytask");

		$where['id'] = $applytask_id;
		$applytaskInfo = $m_applytask->where($where)->find();

		$opencode = array();
		$guess = array();

		$opencode = explode(',', $applytaskInfo['opencode']);//开奖信息
		$guess = explode('-', $applytaskInfo['user_guess']);//用户投注

		// 判定公式
		switch ($guess['0']) {
			case 0://整合页面
				switch ($guess['1']) {
					case 0://总和
						$total = $opencode['0']+$opencode['1']+$opencode['2']+$opencode['3']+$opencode['4'];
						$res = userwinlose($applytask_id,$total,$guess['2'],1);
						break;

					case 1://第一球
						$ball = $opencode['0'];
						$res = userwinlose($applytask_id,$ball,$guess['2'],2);
						break;

					case 2://第二球
						$ball = $opencode['1'];
						$res = userwinlose($applytask_id,$ball,$guess['2'],2);
						break;

					case 3://第三球
						$ball = $opencode['2'];
						$res = userwinlose($applytask_id,$ball,$guess['2'],2);
						break;

					case 4://第四球
						$ball = $opencode['3'];
						$res = userwinlose($applytask_id,$ball,$guess['2'],2);
						break;

					case 5://第五球
						$ball = $opencode['4'];
						$res = userwinlose($applytask_id,$ball,$guess['2'],2);
						break;
					
					default:
						# code...
						break;
				}
				break;

			case 1://第一球
				switch ($guess['1']) {
					case 0://总和
						$total = $opencode['0']+$opencode['1']+$opencode['2']+$opencode['3']+$opencode['4'];
						$res = userwinlose($applytask_id,$total,$guess['2'],1);
						break;

					case 1://第一球
						$ball = $opencode['0'];
						$res = userwinlose($applytask_id,$ball,$guess['2'],2);
						break;
					
					default:
						# code...
						break;
				}
				break;

			case 2://第二球
				switch ($guess['1']) {
					case 0://总和
						$total = $opencode['0']+$opencode['1']+$opencode['2']+$opencode['3']+$opencode['4'];
						$res = userwinlose($applytask_id,$total,$guess['2'],1);
						break;

					case 2://第二球
						$ball = $opencode['1'];
						$res = userwinlose($applytask_id,$ball,$guess['2'],2);
						break;
					
					default:
						# code...
						break;
				}
				break;

			case 3://第三球
				switch ($guess['1']) {
					case 0://总和
						$total = $opencode['0']+$opencode['1']+$opencode['2']+$opencode['3']+$opencode['4'];
						$res = userwinlose($applytask_id,$total,$guess['2'],1);
						break;

					case 3://第三球
						$ball = $opencode['2'];
						$res = userwinlose($applytask_id,$ball,$guess['2'],2);
						break;
					
					default:
						# code...
						break;
				}
				break;

			case 4://第四球
				switch ($guess['1']) {
					case 0://总和
						$total = $opencode['0']+$opencode['1']+$opencode['2']+$opencode['3']+$opencode['4'];
						$res = userwinlose($applytask_id,$total,$guess['2'],1);
						break;

					case 4://第四球
						$ball = $opencode['3'];
						$res = userwinlose($applytask_id,$ball,$guess['2'],2);
						break;
					
					default:
						# code...
						break;
				}
				break;

			case 5://第五球
				switch ($guess['1']) {
					case 0://总和
						$total = $opencode['0']+$opencode['1']+$opencode['2']+$opencode['3']+$opencode['4'];
						$res = userwinlose($applytask_id,$total,$guess['2'],1);
						break;

					case 5://第五球
						$ball = $opencode['4'];
						$res = userwinlose($applytask_id,$ball,$guess['2'],2);
						break;
					
					default:
						# code...
						break;
				}
				break;
			
			default:
				# code...
				break;
		}
		

		return $res;
	}




	/**
	 * [userwinlose description]
	 * @param  [type] $applytask_id     [applytask id]
	 * @param  [type] $ball     		[第几求]
	 * @param  [type] $one_dxds 		[大小单双]
	 * @return [type] $type          	[1:总和 2：单球]
	 */
	function userwinlose($applytask_id,$ball,$one_dxds,$type){
		$m_applytask = M("Applytask");
		$m_user = M('User');

		$where['id'] = $applytask_id;

		$applytaskInfo = $m_applytask->where($where)->find();
		if ($type == 1 ) {
			if ($one_dxds == 0) {//大
				if ($ball > 25) {
					$save['winlose'] = 1;
				}else{
					$save['winlose'] = 0;
				}
				
			}elseif ($one_dxds == 1) {//小
				if ($ball <= 25) {
					$save['winlose'] = 1;
				}else{
					$save['winlose'] = 0;
				}
			}elseif ($one_dxds == 2) {//单
				if ($ball % 2 == 1) {
					$save['winlose'] = 1;
				}else{
					$save['winlose'] = 0;
				}
			}elseif ($one_dxds == 3) {//双
				if ($ball % 2 == 0) {
					$save['winlose'] = 1;
				}else{
					$save['winlose'] = 0;
				}
			}
		}elseif($type == 2){
			if ($one_dxds == 0) {//大
				if ($ball > 4) {
					$save['winlose'] = 1;
				}else{
					$save['winlose'] = 0;
				}
				
			}elseif ($one_dxds == 1) {//小
				if ($ball <= 4) {
					$save['winlose'] = 1;
				}else{
					$save['winlose'] = 0;
				}
			}elseif ($one_dxds == 2) {//单
				if ($ball % 2 == 1) {
					$save['winlose'] = 1;
				}else{
					$save['winlose'] = 0;
				}
			}elseif ($one_dxds == 3) {//双
				if ($ball % 2 == 0) {
					$save['winlose'] = 1;
				}else{
					$save['winlose'] = 0;
				}
			}
		}
		
		$save['status'] = 1;
		$m_applytask->where($where)->save($save);

		//修改用户任务积分、添加任务积分记录、修改用户中心积分、添加中心积分记录
		$where_user['id'] = $applytaskInfo['user_id'];
		$userInfo = $m_user->where($where_user)->find();

		// 添加二级用户 三级用户动态秘钥赠送(无论输赢)
		giveDynamic(1,$userInfo['top_id'],$userInfo['account']);

		if ($save['winlose'] == 1) {
			

			// $old_task = $userInfo['score_task'];
			// $change_task = C('registerScore');
			// $new_task = $old_task + $change_task;

			$old_center = $userInfo['score_center'];
			$change_center = 1.998 * $applytaskInfo['num'];
			$new_center = $old_center + $change_center;

			// $save_user['score_task'] = $new_task;
			$save_user['score_center'] = $new_center;
			$res = $m_user->where($where_user)->save($save_user);
			if ($res) {
				
				// addTaskTable($userInfo['id'],$old_task,$change_task,$new_task,'娱乐平台盈利返还'.$change_task);
				addCenterTable($userInfo['id'],$old_center,$change_center,$new_center,'娱乐平台盈利，新增'.$change_center);
				
			}
			$return_result = 1;
		}else{
			$return_result = 0;
		}

		return $return_result;
	}


	/**
	 * [giveDynamic 多级用户赠送动态秘钥]
	 * @return [type] [description]
	 */
	function giveDynamic($level,$top_id,$account){
		if ($level > 3) {
			return;
		}else{

			$m_user = M('User');

			$where['id'] = $top_id;
			$userInfo = $m_user->where($where)->find();

			if ($level >= 2) {
				$topNum = C($level.'_topNum');//直推人数
				$dynamicNum = C($level.'_dynamicNum');//赠送动态秘钥

				if ($userInfo['top_num'] >= $topNum) {
					$old_key = $userInfo['key_dynamic'];
			        $change_key = $dynamicNum;
			        $new_key = $old_key + $change_key;

			        $save['key_dynamic'] = $new_key;
			        $res = $m_user->where($where)->save($save);
			        if ($res) {
			        	//添加动态秘钥记录
	        			addKeyDynamicTable($userInfo['id'],$old_key,$change_key,$new_key,$account.'用户完成任务赠送秘钥'.$change_key);
			        }

				}

			}

			$level++;
			giveDynamic($level,$userInfo['top_id'],$account);

	    }
	}

	//验证手机号格式
	function isMobile($mobile) {
		
		if (!is_numeric($mobile)) {
	        return false;
	    }
	    return preg_match('#^1[\d]{10}$#', $mobile) ? true : false;
	
	}


	/**
	 * [topPeople 添加直推人数]
	 * @param  [type] $top_id [description]
	 * @return [type]         [description]
	 */
	function topPeople($top_id){
		$where['id'] = $top_id;
		$m_user = M('User');
		$res = $m_user->where($where)->setInc('top_num');
		return;
	}

	/**
	 * [teamPeople 添加团队人数]
	 * @param  [type] $top_id [description]
	 * @return [type]         [description]
	 */
	function teamPeople($top_id){
		$where['id'] = $top_id;
		$m_user = M('User');
		$res = $m_user->where($where)->setInc('team_num');
		
		$userInfo = $m_user->where($where)->find();
		if (!empty($userInfo['top_id'])) {
			teamPeople($userInfo['top_id']);
		}else{
			return;
		}
		
	}


	/**
	 * [addCenterTable 中心积分记录]
	 * @param [type]  $uid        [用户id]
	 * @param [type]  $old_money  [原来积分]
	 * @param [type]  $money      [变动积分]
	 * @param [type]  $new_money  [新积分]
	 * @param [type]  $remark     [备注]
	 * @param integer $type       [1：加 2：减]
	 * @param integer $poundage   [手续费]
	 */
	function addCenterTable($uid,$old_money,$money,$new_money,$remark,$type = 1,$poundage = 0 ){
		$createtime = time();
		$m_center = M('Center');

		$add['order'] = date('Ymd').$uid.$createtime;
		$add['user_id'] = $uid;
		$add['old_money'] = $old_money;
		$add['type'] = $type;
		$add['money'] = $money;
		$add['new_money'] = $new_money;
		$add['poundage'] = $poundage;
		$add['createtime'] = $createtime;
		$add['remark'] = $remark;

		$res = $m_center->add($add);

		return;
	}


	/**
	 * [addCenterTable 添加任务积分记录]
	 * @param [type]  $uid        [用户id]
	 * @param [type]  $old_money  [原来积分]
	 * @param [type]  $money      [变动积分]
	 * @param [type]  $new_money  [新积分]
	 * @param [type]  $remark     [备注]
	 * @param integer $type       [1：加 2：减]
	 * @param integer $poundage   [手续费]
	 */
	function addTaskTable($uid,$old_money,$money,$new_money,$remark,$type = 1,$poundage = 0){
		$createtime = time();
		$m_task = M('Task');

		$add['order'] = date('Ymd').$uid.$createtime;
		$add['user_id'] = $uid;
		$add['old_money'] = $old_money;
		$add['type'] = $type;
		$add['money'] = $money;
		$add['new_money'] = $new_money;
		$add['poundage'] = $poundage;
		$add['createtime'] = $createtime;
		$add['remark'] = $remark;

		$res = $m_task->add($add);
		
		return;
	}

	/**
	 * [addTradingTable 添加交易积分记录]
	 * @param [type]  $uid        [用户id]
	 * @param [type]  $old_money  [原来积分]
	 * @param [type]  $money      [变动积分]
	 * @param [type]  $new_money  [新积分]
	 * @param [type]  $remark     [备注]
	 * @param integer $type       [1：加 2：减]
	 * @param integer $poundage   [手续费]
	 */
	function addTradingTable($uid,$old_money,$money,$new_money,$remark,$type = 1,$poundage = 0){
		$createtime = time();
		$m_trading = M('Trading');

		$add['order'] = date('Ymd').$uid.$createtime;
		$add['user_id'] = $uid;
		$add['old_money'] = $old_money;
		$add['type'] = $type;
		$add['money'] = $money;
		$add['new_money'] = $new_money;
		$add['poundage'] = $poundage;
		$add['createtime'] = $createtime;
		$add['remark'] = $remark;

		$res = $m_trading->add($add);
		
		return;
	}


	/**
	 * [addKeyTaskTable 添加任务秘钥记录]
	 * @param [type]  $uid        [用户id]
	 * @param [type]  $old_money  [原来任务秘钥]
	 * @param [type]  $money      [变动秘钥]
	 * @param [type]  $new_money  [新秘钥]
	 * @param [type]  $remark     [备注]
	 * @param integer $type       [1：加 2：减]
	 */
	function addKeyTaskTable($uid,$old_money,$money,$new_money,$remark,$type = 1){
		$createtime = time();
		$m_keytask = M('Key_task');

		$add['user_id'] = $uid;
		$add['old_money'] = $old_money;
		$add['type'] = $type;
		$add['money'] = $money;
		$add['new_money'] = $new_money;
		$add['createtime'] = $createtime;
		$add['remark'] = $remark;

		$res = $m_keytask->add($add);
		return;
	}


	/**
	 * [addKeyDynamicTable 添加动态秘钥记录]
	 * @param [type]  $uid        [用户id]
	 * @param [type]  $old_money  [原来动态秘钥]
	 * @param [type]  $money      [变动秘钥]
	 * @param [type]  $new_money  [新秘钥]
	 * @param [type]  $remark     [备注]
	 * @param integer $type       [1：加 2：减]
	 */
	function addKeyDynamicTable($uid,$old_money,$money,$new_money,$remark,$type = 1){
		$createtime = time();
		$m_keydynamic = M('Key_dynamic');

		$add['user_id'] = $uid;
		$add['old_money'] = $old_money;
		$add['type'] = $type;
		$add['money'] = $money;
		$add['new_money'] = $new_money;
		$add['createtime'] = $createtime;
		$add['remark'] = $remark;

		$res = $m_keydynamic->add($add);
		return;
	}


	/**
	 * [givingDynamicKey 赠送动态秘钥]
	 * @param  [type] $top_id 	[推荐人ID]
	 * @param  [type] $account 	[激活账号]
	 * @return [type]         	[description]
	 */
	function givingDynamicKey($top_id,$account){
		$m_user = M('User');
		$giveDynamic = C('giveDynamic');//赠送动态秘钥

		$where['id'] = $top_id;
		$userInfo = $m_user->where($where)->find();

		$old_key = $userInfo['key_dynamic'];
        $change_key = $giveDynamic;
        $new_key = $old_key + $change_key;

        $save['key_dynamic'] = $new_key;
        $res = $m_user->where($where)->save($save);
        if ($res) {
        	//添加动态秘钥记录
        	addKeyDynamicTable($userInfo['id'],$old_key,$change_key,$new_key,$account.'用户激活账号赠送'.$change_key);
        }
        return;
	}


	/**
	 * [addGivingTable 添加赠送记录]
	 * @param [type]  $uid             [赠送人账号id]
	 * @param [type]  $receive_account [接收账户]
	 * @param [type]  $receive_uid     [接收账户id]
	 * @param integer $num             [赠送数量]
	 */
	function addGivingTable($uid,$receive_account,$receive_uid,$num=0){
		$createtime = time();
		$m_giving = M('Giving');

		$add['user_id'] = $uid;
		$add['receive_account'] = $receive_account;
		$add['receive_uid'] = $receive_uid;
		$add['num'] = $num;
		$add['createtime'] = $createtime;
		
		$res = $m_giving->add($add);
		return;
	}